package org.goldfish.mapreduce.wordcount;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
@Slf4j
public class WordCountReducer
        extends Reducer<Text, IntWritable, Text, IntWritable> {
    private final IntWritable result = new IntWritable();

    public void reduce(
            Text key, Iterable<IntWritable> values,
            Context context)
            throws java.io.IOException, InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
            log.info("key:{}--val:{}", key, val);
            sum += val.get();
        }
        log.info("sum for key {}:{}",  key, sum);
        result.set(sum);
        context.write(key, result);
    }
}
